package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author liu
 * @version 1.0.0
 * @createTime 2020年10月09日
 * @Description 菜单接口实现
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> getMenuList(String username) {
        //获取主菜单
        List<Menu> parentMenu = menuDao.getParentMenu(username);
        //加入子菜单
        for (Menu menu : parentMenu) {
            //获取父菜单的id
            Integer parentMenuId = menu.getId();
            List<Menu> childrenMenu = menuDao.getChildrenMenuByParentMenuId(parentMenuId, username);
            menu.setChildren(childrenMenu);
        }
        return parentMenu;
    }

    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[菜单分页查询service]data:{}", queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Menu findById(Integer id) {
        log.info("[菜单查询数据service]id={}", id);

        return menuDao.selectById(id);
    }

    @Override
    public void add(Menu menu) {
        menuDao.insert(menu);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.updata(menu);
    }

    @Override
    public void delete(Integer id) {
        log.info("[菜单删除service]id={}", id);
        menuDao.deleteRoleIdBymenuId(id);
        menuDao.delete(id);
    }

    @Override
    public List<Menu> findAll() {

        return menuDao.selectAll();
    }

}
